home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 July / Chip Temmuz 2004.iso / program / antispam / RazorAgent_SDK / razor-agents-sdk-2.03.exe / URI-1.19 / t / abs.t next >
Encoding:
Text File  |  1999-07-22  |  5.6 KB  |  170 lines

  1. print "1..45\n";
  2.  
  3. # This test the resolution of abs path for all examples given
  4. # in the "Uniform Resource Identifiers (URI): Generic Syntax" document.
  5.  
  6. use URI;
  7. $base = "http://a/b/c/d;p?q";
  8. $testno = 1;
  9.  
  10. while (<DATA>) {
  11.    #next if 1 .. /^C\.\s+/;
  12.    #last if /^D\.\s+/;
  13.    next unless /\s+(\S+)\s*=\s*(.*)/;
  14.    my $uref = $1;
  15.    my $expect = $2;
  16.    $expect =~ s/\(current document\)/$base/;
  17.    #print "$uref => $expect\n";
  18.  
  19.    my $bad;
  20.    my $u = URI->new($uref, $base);
  21.    if ($u->abs($base)->as_string ne $expect) {
  22.        $bad++;
  23.        my $abs = $u->abs($base)->as_string;
  24.        print qq(URI->new("$uref")->abs("$base") ==> "$abs"\n);
  25.    }
  26.  
  27.    # Let's test another version of the same thing
  28.    $u = URI->new($uref);
  29.    my $b = URI->new($base);
  30.    if ($u->abs($b,1) ne $expect && $uref !~ /^http:/) {
  31.        $bad++;
  32.        print qq(URI->new("$uref")->abs(URI->new("$base"), 1)\n);
  33.    }
  34.  
  35.    # Let's try the other way
  36.    $u = URI->new($expect)->rel($base)->as_string;
  37.    if ($u ne $uref) {
  38.        push(@rel_fail, qq($testno: URI->new("$expect", "$base")->rel ==> "$u" (not "$uref")\n));
  39.    }
  40.  
  41.    print "not " if $bad;
  42.    print "ok ", $testno++, "\n";
  43. }
  44.  
  45. if (@rel_fail) {
  46.     print "\n\nIn the following cases we did not get back to where we started with rel()\n";
  47.     print @rel_fail;
  48. }
  49.  
  50.  
  51.  
  52. __END__
  53.  
  54. Network Working Group                            T. Berners-Lee, MIT/LCS
  55. INTERNET-DRAFT                                 R. Fielding,  U.C. Irvine
  56. draft-fielding-uri-syntax-02              L. Masinter, Xerox Corporation
  57. Expires six months after publication date                  March 4, 1998
  58.  
  59.  
  60.           Uniform Resource Identifiers (URI): Generic Syntax
  61.  
  62. [...]
  63.  
  64. C. Examples of Resolving Relative URI References
  65.  
  66.    Within an object with a well-defined base URI of
  67.  
  68.       http://a/b/c/d;p?q
  69.  
  70.    the relative URIs would be resolved as follows:
  71.  
  72. C.1.  Normal Examples
  73.  
  74.       g:h           =  g:h
  75.       g             =  http://a/b/c/g
  76.       ./g           =  http://a/b/c/g
  77.       g/            =  http://a/b/c/g/
  78.       /g            =  http://a/g
  79.       //g           =  http://g
  80.       ?y            =  http://a/b/c/?y
  81.       g?y           =  http://a/b/c/g?y
  82.       #s            =  (current document)#s
  83.       g#s           =  http://a/b/c/g#s
  84.       g?y#s         =  http://a/b/c/g?y#s
  85.       ;x            =  http://a/b/c/;x
  86.       g;x           =  http://a/b/c/g;x
  87.       g;x?y#s       =  http://a/b/c/g;x?y#s
  88.       .             =  http://a/b/c/
  89.       ./            =  http://a/b/c/
  90.       ..            =  http://a/b/
  91.       ../           =  http://a/b/
  92.       ../g          =  http://a/b/g
  93.       ../..         =  http://a/
  94.       ../../        =  http://a/
  95.       ../../g       =  http://a/g
  96.  
  97. C.2.  Abnormal Examples
  98.  
  99.    Although the following abnormal examples are unlikely to occur in
  100.    normal practice, all URI parsers should be capable of resolving them
  101.    consistently.  Each example uses the same base as above.
  102.  
  103.    An empty reference refers to the start of the current document.
  104.  
  105.       <>            =  (current document)
  106.  
  107.    Parsers must be careful in handling the case where there are more
  108.    relative path ".." segments than there are hierarchical levels in
  109.    the base URI's path.  Note that the ".." syntax cannot be used to
  110.    change the authority component of a URI.
  111.  
  112.       ../../../g    =  http://a/../g
  113.       ../../../../g =  http://a/../../g
  114.  
  115.    In practice, some implementations strip leading relative symbolic
  116.    elements (".", "..") after applying a relative URI calculation, based
  117.    on the theory that compensating for obvious author errors is better
  118.    than allowing the request to fail.  Thus, the above two references
  119.    will be interpreted as "http://a/g" by some implementations.
  120.  
  121.    Similarly, parsers must avoid treating "." and ".." as special when
  122.    they are not complete components of a relative path.
  123.  
  124.       /./g          =  http://a/./g
  125.       /../g         =  http://a/../g
  126.       g.            =  http://a/b/c/g.
  127.       .g            =  http://a/b/c/.g
  128.       g..           =  http://a/b/c/g..
  129.       ..g           =  http://a/b/c/..g
  130.  
  131.    Less likely are cases where the relative URI uses unnecessary or
  132.    nonsensical forms of the "." and ".." complete path segments.
  133.  
  134.       ./../g        =  http://a/b/g
  135.       ./g/.         =  http://a/b/c/g/
  136.       g/./h         =  http://a/b/c/g/h
  137.       g/../h        =  http://a/b/c/h
  138.       g;x=1/./y     =  http://a/b/c/g;x=1/y
  139.       g;x=1/../y    =  http://a/b/c/y
  140.  
  141.    All client applications remove the query component from the base URI
  142.    before resolving relative URIs.  However, some applications fail to
  143.    separate the reference's query and/or fragment components from a
  144.    relative path before merging it with the base path.  This error is
  145.    rarely noticed, since typical usage of a fragment never includes the
  146.    hierarchy ("/") character, and the query component is not normally
  147.    used within relative references.
  148.  
  149.       g?y/./x       =  http://a/b/c/g?y/./x
  150.       g?y/../x      =  http://a/b/c/g?y/../x
  151.       g#s/./x       =  http://a/b/c/g#s/./x
  152.       g#s/../x      =  http://a/b/c/g#s/../x
  153.  
  154.    Some parsers allow the scheme name to be present in a relative URI
  155.    if it is the same as the base URI scheme.  This is considered to be
  156.    a loophole in prior specifications of partial URIs [RFC1630]. Its
  157.    use should be avoided.
  158.  
  159.       http:g        =  http:g
  160.       http:         =  http:
  161.  
  162.  
  163. ==========================================================================
  164.  
  165. Some extra tests for good measure...
  166.  
  167.       #foo?        = (current document)#foo?
  168.       ?#foo        = http://a/b/c/?#foo
  169.  
  170.